package com.ym.jiujiangeyou.fragment;

import java.util.ArrayList;

import java.util.List;

import com.ym.jiujiangeyou.FoodListActivity;
import com.ym.jiujiangeyou.HotelListActivity;
import com.ym.jiujiangeyou.IntroduceActivity;
import com.ym.jiujiangeyou.R;
import com.ym.jiujiangeyou.SceneryDetails;
import com.ym.jiujiangeyou.SceneryListActivity;
import com.ym.jiujiangeyou.TrafficActivity;
import com.ym.jiujiangeyou.adapter.IndexViewPageAdapter;
import com.ym.jiujiangeyou.bitmap.BitmapUtil;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
 /**   
 * @author 叶铭   
 * @email yeming_1001@163.com
 * @version V1.0   
 */
public class IndexFragment extends Fragment implements OnClickListener ,OnPageChangeListener{

	public IndexFragment() {
	}

	private ViewPager viewPager  ;
	private IndexViewPageAdapter pageAdapter ;
	private int width ;
	private int height ;
	private List<View> listViews ;
	// 引导图片资源
			private static final int[] pics = { R.drawable.page3, R.drawable.page1,
					R.drawable.page2, R.drawable.page6 };
			// 底部小店图片
			private ImageView[] dots;
			// 记录当前选中位置
			private int currentIndex;
			private View view;
			private RelativeLayout relativeLayout ;
			
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.index, null, false) ;
		TextView title_tex = (TextView) view.findViewById(R.id.title_text) ;
		
		//注册监听
		view.findViewById(R.id.item_introduce).setOnClickListener(this) ;
		view.findViewById(R.id.item_scenery).setOnClickListener(this) ;
		view.findViewById(R.id.item_hotel).setOnClickListener(this) ;
		view.findViewById(R.id.item_traffic).setOnClickListener(this) ;
		view.findViewById(R.id.item_food).setOnClickListener(this) ;
		
		title_tex.setText(R.string.tab_index) ;
		//等到屏幕的大小
		 width	= getActivity().getWindowManager().getDefaultDisplay().getWidth();
		 height = getActivity().getWindowManager().getDefaultDisplay().getHeight() ;
		 //以670*240的图片为例,正常中不要这样用
	//	 height	=width*240/670;
		 height = height / 4  ;
		 relativeLayout = (RelativeLayout) view.findViewById(R.id.relative);
		 LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) relativeLayout.getLayoutParams();
		 layoutParams.width = width;
		 layoutParams.height = height;
		 relativeLayout.setLayoutParams(layoutParams);
		 
		listViews = new ArrayList<View>();

		LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT);

		// 初始化引导图片列表
		for (int i = 0; i < pics.length; i++) {
			ImageView iv = new ImageView(getActivity());
			iv.setLayoutParams(mParams);
			//改变大小
//			iv.setImageResource(pics[i]);
			iv.setImageBitmap(BitmapUtil.zoomImage(BitmapFactory.decodeResource(getResources(), pics[i]), width, height));
			listViews.add(iv);
		}
		viewPager = (ViewPager) view.findViewById(R.id.viewpager);
		
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
		/*params.leftMargin=10;     //  viewpage的间距
		params.topMargin=10;
		params.rightMargin=10;*/   
		viewPager.setLayoutParams(params);
		
		// 初始化Adapter
		pageAdapter = new IndexViewPageAdapter(listViews) ;
		viewPager.setAdapter(pageAdapter) ;
		// 绑定回调
	   viewPager.setOnPageChangeListener(this) ;

		// 初始化底部小点
		initDots();
		return view;
		
	}

	private void initDots() {
		LinearLayout ll = (LinearLayout) view.findViewById(R.id.ll);
		dots = new ImageView[pics.length];

		// 循环取得小点图片
		for (int i = 0; i < pics.length; i++) {
			dots[i] = (ImageView) ll.getChildAt(i);
			dots[i].setEnabled(true);// 都设为灰色
			dots[i].setOnClickListener(this);
			dots[i].setTag(i);// 设置位置tag，方便取出与当前位置对应
		}

		currentIndex = 0;
		dots[currentIndex].setEnabled(false);// 设置为白色，即选中状态
	}
	
	/**
	 * 设置当前的引导页
	 */
	private void setCurView(int position) {
		if (position < 0 || position >= pics.length) {
			return;
		}

		viewPager.setCurrentItem(position);
	}
	
	/**
	 * 这只当前引导小点的选中
	 */
	private void setCurDot(int positon) {
		if (positon < 0 || positon > pics.length - 1 || currentIndex == positon) {
			return;
		}

		dots[positon].setEnabled(false);
		dots[currentIndex].setEnabled(true);

		currentIndex = positon;
	}
	
	// 当滑动状态改变时调用
		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		// 当当前页面被滑动时调用
		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {

			viewPager.requestDisallowInterceptTouchEvent(true);
		}

		// 当新的页面被选中时调用
		@Override
		public void onPageSelected(int arg0) {
			// 设置底部小点选中状态
			setCurDot(arg0);
		}

		@Override
		public void onClick(View view) {
			
			Intent intent = null ;
			switch (view.getId()) {
			case R.id.item_introduce:
			//	Toast.makeText(getActivity(), "dd", 1).show() ;
				intent = new Intent(getActivity(), IntroduceActivity.class) ;
				startActivity(intent) ;
				/*getActivity().overridePendingTransition(R.anim.push_left_in,
						R.anim.push_left_out) ;*/
				break;
			case R.id.item_scenery:
				intent = new Intent(getActivity(), SceneryListActivity.class) ;
			//	Intent intent1 = new Intent(getActivity(), SceneryDetails.class) ;
				startActivity(intent) ;
				/*getActivity().overridePendingTransition(R.anim.push_left_in,
						R.anim.push_left_out) ;*/
				break ;
			case R.id.item_hotel:
				intent = new Intent(getActivity(), HotelListActivity.class) ;
				//	Intent intent1 = new Intent(getActivity(), SceneryDetails.class) ;
					startActivity(intent) ;
			//		getActivity().overridePendingTransition(R.anim.zoom_in,R.anim.zoom_out) ;
				break ;
			case R.id.item_traffic:
				intent = new Intent(getActivity(), TrafficActivity.class) ;
				//	Intent intent1 = new Intent(getActivity(), SceneryDetails.class) ;
					startActivity(intent) ;
				//	getActivity().overridePendingTransition(R.anim.zoom_in,R.anim.zoom_out) ;
				break ;
			case R.id.item_food:
				intent = new Intent(getActivity(), FoodListActivity.class) ;
					startActivity(intent) ;
				//	getActivity().overridePendingTransition(R.anim.zoom_in,R.anim.zoom_out) ;
				break ;
			default:
				break;
			}
			
		}

}
